<?php
    class PropertytypeController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
            $this->lng_id = '1';
           
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'propertytype','default');
        }

        public function viewAction() {
            if ($this->_getParam('prtyp_id') && $this->_getParam('prtyp_id') != '') {
                $prtyp_id = $this->_getParam('prtyp_id');
            } else {
                $this->_helper->redirector('list', 'propertytype', 'default');
            }
            $propertytypeModel = new Model_Propertytype();
            $propertytypealldata = $propertytypeModel->fetchEntryById($prtyp_id);
            $this->view->propertytypealldata = $propertytypealldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $propertytypesCount = array();


            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
               
                
            } 

            $propertytypeModel = new Model_Propertytype();
            $propertytypesData = $propertytypeModel->fetchLISTPropertytype($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $propertytypesCount = $propertytypeModel->fetchLISTPropertytype($where);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal(count($propertytypesCount));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'propertytype', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->propertytypesData = $propertytypesData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $propertytypeModel->getStatusArray();
        }
        public function deleteAction($ids = Array()) {
               //Load propertytype Model 
               $propertytypeModel = new Model_Propertytype();
               if ($this->_getParam('prtyp_ref_id') && $this->_getParam('prtyp_ref_id') != '') {
                   $prtyp_ref_id = $this->_getParam('prtyp_ref_id');
                   $propertytypeModel->deletePropertytype($prtyp_ref_id);
                   $this->addSuccessMessage('Record Successfully Deleted');
                  $this->_helper->redirector('list', 'propertytype', 'default');
               } else {
                   $propertytypeModel->deletePropertytype($ids);
                  $this->addSuccessMessage('Record Successfully Deleted');
                   $this->_helper->redirector('list', 'propertytype', 'default');
               }
           }
        public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $propertytypeData = Array();
            $this->view->mode = 'Add';

            //Load propertytype Model
            $propertytypeModel = new Model_Propertytype();

            //Load propertytype Form
            //$propertytypeForm = new Form_PropertytypeForm();

            $languageModel = new Model_Language();
            /*$languageArray = $languageModel->fetchlanguageArray();
            $propertytypeForm->prtyp_ln_id->addMultiOptions($languageArray);
             */
            //Load the status array for the select box in form 
           /* $statusArray = $propertytypeModel->getStatusArray();
            $propertytypeForm->prtyp_status->addMultiOptions($statusArray);
            */
            
            $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['prtyp_title'] = "";
                $languages[$_lan['lang_id']]['prtyp_name'] = "";
                $languages[$_lan['lang_id']]['prtyp_ref_id'] = "";
                $languages[$_lan['lang_id']]['prtyp_id'] = "";
            }
        
            //Check for the propertytype Parameter
            $propertytypeData['prtyp_ref_id'] = $this->_request->getParam('prtyp_ref_id');
            $prtyp_ref_id = (!empty($propertytypeData['prtyp_ref_id'])) ? $propertytypeData['prtyp_ref_id'] : null;
            
            
            
            if (!empty($prtyp_ref_id)) {
            //Set the Mode to Edit
                $this->view->mode = 'Edit';
                $data = $propertytypeModel->fetchEntryByREFId($prtyp_ref_id);
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['prtyp_ln_id'], $languages)) {
                        $languages[$_desc['prtyp_ln_id']]['prtyp_title'] = $_desc['prtyp_title'];
                        $languages[$_desc['prtyp_ln_id']]['prtyp_name'] = $_desc['prtyp_name'];
                        $languages[$_desc['prtyp_ln_id']]['prtyp_id'] = $_desc['prtyp_id'];
                        $languages[$_desc['prtyp_ln_id']]['prtyp_ref_id'] = $prtyp_ref_id;
                    }
                }
            }
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if($postedData['prtyp_name']== ''){
                    $errorMessageArray[] = 'Please enter type name.';
                }
                if($postedData['1_prtyp_title']== ''){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                        $languages[$key]['prtyp_title'] = $postedData[$_lang['lang_id'].'_prtyp_title'];
                        $languages[$key]['prtyp_name'] = $postedData['prtyp_name'];
                    }
                }
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                      /*  if ($this->view->mode == 'Add') {
                            $propertytypeData['prtyp_createddate'] = date('Y-m-d H:i:s');
                        }
                        //propertytype save
                        $propertytypeData['prtyp_id'] = $prtyp_id;
                        $propertytypeData['prtyp_title'] = $postedData['prtyp_title'];
                        $propertytypeData['prtyp_name'] = $postedData['prtyp_name'];
                        $propertytypeData['prtyp_ln_id'] = $postedData['prtyp_ln_id'];
                        $propertytypeData['prtyp_updateddate'] = date('Y-m-d H:i:s');
                        $propertytypeData['prtyp_status'] = $postedData['prtyp_status'];
                        $propertytypeModel->save($propertytypeData, 'prtyp_id');*/
                        
                         if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $propertytypeData['prtyp_title'] = $postedData[$_lang['lang_id'].'_prtyp_title'];
                                $propertytypeData['prtyp_name'] = $postedData['prtyp_name'];
                                $propertytypeData['prtyp_createddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_ln_id'] = $_lang['lang_id'];
                                $propertytypeData['prtyp_status'] = 1;
                                $propertytypeData['prtyp_ref_id'] = $_lang['prtyp_ref_id'];
                                $last_id = $propertytypeModel->save($propertytypeData, 'prtyp_id');
                                $propertytypeData1['prtyp_id'] = $last_id;
                                $propertytypeData1['prtyp_ref_id'] = $last_id;
                                $propertytypeModel->save($propertytypeData1, 'prtyp_id');
                            }else{
                                $propertytypeData['prtyp_title'] = $postedData[$_lang['lang_id'].'_prtyp_title'];
                                $propertytypeData['prtyp_name'] = $postedData['prtyp_name'];
                                $propertytypeData['prtyp_createddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_ln_id'] = $_lang['lang_id'];
                                $propertytypeData['prtyp_status'] = 1;
                                $propertytypeData['prtyp_ref_id'] = $last_id;
                                if($propertytypeData['prtyp_title']!= '' ){
                                    $propertytypeModel->save($propertytypeData, 'prtyp_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $propertytypeData['prtyp_id'] = $_lang['prtyp_id'];
                                $propertytypeData['prtyp_title'] = $postedData[$_lang['lang_id'].'_prtyp_title'];
                                $propertytypeData['prtyp_name'] = $postedData['prtyp_name'];
                                $propertytypeData['prtyp_ln_id'] = $_lang['lang_id'];
                                $propertytypeData['prtyp_status'] = 1;
                                $propertytypeData['prtyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_ref_id'] = $prtyp_ref_id;
                                $propertytypeModel->save($propertytypeData, 'prtyp_id');
                                
                            }else{
                                $propertytypeData['prtyp_id'] = $_lang['prtyp_id'];
                                $propertytypeData['prtyp_title'] = $postedData[$_lang['lang_id'].'_prtyp_title'];
                                $propertytypeData['prtyp_name'] = $postedData['prtyp_name'];
                                $propertytypeData['prtyp_ln_id'] = $_lang['lang_id'];
                                $propertytypeData['prtyp_status'] = 1;
                                $propertytypeData['prtyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertytypeData['prtyp_ref_id'] = $prtyp_ref_id;
                                $propertytypeModel->save($propertytypeData, 'prtyp_id');
                                
                            }
                        }
                     
                   }
                        
                         if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'Propertytype add successfully';
                            } else {
                                $successmessage = 'Propertytype updated successfully';
                            }
                            $this->addSuccessMessage($successmessage);
                            $this->_helper->redirector('list', 'propertytype', 'default');
                        }
                    }
                
            }
            $this->view->languages = $languages;
            $this->view->propertytypeid = $prtyp_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            //$this->view->form = $propertytypeForm;


    }
        public function subtypelistAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'lang_id';
            $sort = 'asc';
            $propertytypesCount = array();

            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                /*search code here*/
               
                
            } 

            $propertysubtypeModel = new Model_Propertysubtype();
            $propertytypesData = $propertysubtypeModel->fetchLISTPropertySubType($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $propertytypesCount = $propertysubtypeModel->fetchLISTPropertySubType($where);
            
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal(count($propertytypesCount));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'propertytype', 'action' => 'subtypelist', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->propertytypesData = $propertytypesData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $propertysubtypeModel->getStatusArray();
        }
        public function subtypedeleteAction($ids = Array()) {
               //Load propertytype Model 
               $propertysubtypeModel = new Model_Propertysubtype();
               if ($this->_getParam('prstyp_ref_id') && $this->_getParam('prstyp_ref_id') != '') {
                   $prstyp_ref_id = $this->_getParam('prstyp_ref_id');
                   $propertysubtypeModel->deletePropertysubtype($prstyp_ref_id);
                   $this->addSuccessMessage('Record Successfully Deleted');
                  $this->_helper->redirector('subtypelist', 'propertytype', 'default');
               } else {
                   $propertysubtypeModel->deletePropertysubtype($ids);
                   $this->addSuccessMessage('Record Successfully Deleted');
                   $this->_helper->redirector('subtypelist', 'propertytype', 'default');
               }
           }
        public function subtypeformAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $propertysubtypeData = Array();
            $this->view->mode = 'Add';

            //Load propertytype Model
            $propertytypeModel = new Model_Propertytype();
            
             //Load propersubtytype Model
            $propertysubtypeModel = new Model_Propertysubtype();

            //Load propertytype Form
            $propertytypeForm = new Form_PropertysubtypeForm();

            
            $typeArray = $propertytypeModel->fetchPropertytype("prtyp_ln_id = ".$this->lng_id."");
           // $propertytypeForm->prtyp_id->addMultiOptions($typeArray);
            
            $languageModel = new Model_Language();
            /*$languageArray = $languageModel->fetchlanguageArray();
            $propertytypeForm->prstyp_ln_id->addMultiOptions($languageArray);
             
            //Load the status array for the select box in form 
            $statusArray = $propertytypeModel->getStatusArray();
            $propertytypeForm->prstyp_status->addMultiOptions($statusArray);
*/
            $languagesArray = $languageModel->fetchlanguages();
            $languages = Array();
            foreach ($languagesArray as $key => $_lan) {
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']] = $_lan;
                $languages[$_lan['lang_id']]['prstyp_title'] = "";
                $languages[$_lan['lang_id']]['prstyp_name'] = "";
                $languages[$_lan['lang_id']]['prstyp_ref_id'] = "";
                $languages[$_lan['lang_id']]['prstyp_id'] = "";
                $languages[$_lan['lang_id']]['prtyp_id'] = "";
                
            }
            //Check for the propertytype Parameter
            $propertysubtypeData['prstyp_ref_id'] = $this->_request->getParam('prstyp_ref_id');
            $prstyp_ref_id = (!empty($propertysubtypeData['prstyp_ref_id'])) ? $propertysubtypeData['prstyp_ref_id'] : null;
            
            if (!empty($prstyp_ref_id)) {
                //Set the Mode to Edit
                $this->view->mode = 'Edit';
                //Fetch data to be loaded in form 
                $data = $propertysubtypeModel->fetchEntryByREFId($prstyp_ref_id);
                
                foreach ($data as $_desc) {
                    if (array_key_exists($_desc['prstyp_ln_id'], $languages)) {
                        $languages[$_desc['prstyp_ln_id']]['prstyp_title'] = $_desc['prstyp_title'];
                        $languages[$_desc['prstyp_ln_id']]['prstyp_name'] = $_desc['prstyp_name'];
                        $languages[$_desc['prstyp_ln_id']]['prstyp_id'] = $_desc['prstyp_id'];
                        $languages[$_desc['prstyp_ln_id']]['prstyp_ref_id'] = $prstyp_ref_id;
                        $languages[$_desc['prstyp_ln_id']]['prtyp_id'] = $_desc['prtyp_id'];
                        
                    }
                }
            }
            
            //Populate form 
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                
                 //$postedData['1_prtyp_id']== ''
                if($postedData['prstyp_name']== ''){
                     $errorMessageArray[] = 'Please enter name.';
                     $languages[1]['prstyp_name'] = $postedData['prstyp_name'];
                     $languages[1]['prtyp_id'] = $postedData['prtyp_id'];
                } if($postedData['prtyp_id']== ''){
                     $errorMessageArray[] = 'Please select type.';
                     $languages[1]['prstyp_name'] = $postedData['prstyp_name'];
                     $languages[1]['prtyp_id'] = $postedData['prtyp_id'];
                }        
                if($postedData['1_prstyp_title']== '' ){
                    $errorMessageArray[] = 'English value should not be blank.';
                    foreach ($languages as $key => $_lang) {
                        $languages[$key]['prstyp_title'] = $postedData[$_lang['lang_id'].'_prstyp_title'];
                        $languages[$key]['prstyp_name'] = $postedData['prstyp_name'];
                        $languages[$key]['prtyp_id'] = $postedData['prtyp_id'];
                        
                    }
                }
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        /*if ($this->view->mode == 'Add') {
                            $propertysubtypeData['prstyp_createddate'] = date('Y-m-d H:i:s');
                        }
                        //propertytype save
                        $propertysubtypeData['prstyp_id'] = $prstyp_id;
                        $propertysubtypeData['prtyp_id'] = $postedData['prtyp_id'];
                        $propertysubtypeData['prstyp_title'] = $postedData['prstyp_title'];
                        $propertysubtypeData['prstyp_name'] = $postedData['prstyp_name'];
                        $propertysubtypeData['prstyp_ln_id'] = $postedData['prstyp_ln_id'];
                        $propertysubtypeData['prstyp_updateddate'] = date('Y-m-d H:i:s');
                        $propertysubtypeData['prstyp_status'] = $postedData['prstyp_status'];
			$propertysubtypeModel->save($propertysubtypeData, 'prstyp_id');
                         * 
                         */
                        if($this->view->mode == 'Add'){
                        foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $propertysubtypeData['prstyp_title'] = $postedData[$_lang['lang_id'].'_prstyp_title'];
                                $propertysubtypeData['prstyp_name'] = $postedData['prstyp_name'];
                                $propertysubtypeData['prtyp_id'] = $postedData['prtyp_id'];
                                $propertysubtypeData['prstyp_createddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_ln_id'] = $_lang['lang_id'];
                                $propertysubtypeData['prstyp_status'] = 1;
                                $propertysubtypeData['prstyp_ref_id'] = $_lang['prstyp_ref_id'];
                                $last_id = $propertysubtypeModel->save($propertysubtypeData, 'prstyp_id');
                                $propertysubtypeData1['prstyp_id'] = $last_id;
                                $propertysubtypeData1['prstyp_ref_id'] = $last_id;
                                $propertysubtypeModel->save($propertysubtypeData1, 'prstyp_id');
                            }else{
                                $propertysubtypeData['prstyp_title'] = $postedData[$_lang['lang_id'].'_prstyp_title'];
                                $propertysubtypeData['prstyp_name'] = $postedData['prstyp_name'];
                                $propertysubtypeData['prtyp_id'] = $postedData['prtyp_id'];
                                $propertysubtypeData['prstyp_createddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_ln_id'] = $_lang['lang_id'];
                                $propertysubtypeData['prstyp_status'] = 1;
                                $propertysubtypeData['prstyp_ref_id'] = $last_id;
                                if($propertysubtypeData['prstyp_title']!= '' ){
                                    $propertysubtypeModel->save($propertysubtypeData, 'prstyp_id');
                                }
                            }
                        }
                   }
                         else{
                       //edit mode
                       foreach ($languages as $key => $_lang) {
                            if($_lang['lang_id'] == 1){
                                $propertysubtypeData['prstyp_id'] = $_lang['prstyp_id'];
                                $propertysubtypeData['prstyp_title'] = $postedData[$_lang['lang_id'].'_prstyp_title'];
                                $propertysubtypeData['prstyp_name'] = $postedData['prstyp_name'];
                                $propertysubtypeData['prtyp_id'] = $postedData['prtyp_id'];
                                 $propertysubtypeData['prstyp_ln_id'] = $_lang['lang_id'];
                                $propertysubtypeData['prstyp_status'] = 1;
                                $propertysubtypeData['prstyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_ref_id'] = $prstyp_ref_id;
                                $propertysubtypeModel->save($propertysubtypeData, 'prstyp_id');
                                
                            }else{
                                $propertysubtypeData['prstyp_id'] = $_lang['prstyp_id'];
                                $propertysubtypeData['prstyp_title'] = $postedData[$_lang['lang_id'].'_prstyp_title'];
                                $propertysubtypeData['prstyp_name'] = $postedData['prstyp_name'];
                                $propertysubtypeData['prtyp_id'] = $postedData['prtyp_id'];
                                $propertysubtypeData['prstyp_ln_id'] = $_lang['lang_id'];
                                $propertysubtypeData['prstyp_status'] = 1;
                                $propertysubtypeData['prstyp_updateddate'] = date('Y-m-d H:i:s');
                                $propertysubtypeData['prstyp_ref_id'] = $prstyp_ref_id;
                                $propertysubtypeModel->save($propertysubtypeData, 'prstyp_id');
                                
                            }
                        }
                     
                   }
                        
                         if (count($errorMessageArray) > 0) {

                        } else {
                            if ($this->view->mode == 'Add') {
                                $successmessage = 'Property Subtype add successfully';
                            } else {
                                $successmessage = 'Property Subtype updated successfully';
                            }
                            $this->addSuccessMessage($successmessage);
                            $this->_helper->redirector('subtypelist', 'propertytype', 'default');
                        }
                    }
                
            }
            $this->view->propertyType = $typeArray;
            $this->view->languages = $languages;
            $this->view->propertysubtypeid = $prstyp_ref_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $propertytypeForm;


    }
    
       
        
    }?>